<template>
  <el-dialog title="分配权限" :visible="isShow" @close="closeEvent">
    <!-- 树形控件
    :default-checked-keys="premissionList" 默认选中项对应的值,来自于node-key对应的字段 累加的,所以关闭时需要清空
     node-key="id" 指定选中该项后的值
     getCheckedKeys 实例方法 获取被选中的节点的 key 所组成的数组
     setCheckedKeys 通过 keys 设置目前勾选的节点，使用此方法必须设置 node-key 属性
     default-expand-all 展开所以子节点
     show-checkbox 展示复选框
     check-strictly 在显示复选框的情况下,父子不互相关联 默认为false
    -->
    <el-tree
      v-if="bol"
      ref="tree"
      node-key="id"
      :data="list"
      :props="{ label: 'name' }"
      show-checkbox
      default-expand-all
      check-strictly
    />
    <template #footer>
      <div>
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermission } from '@/api/permission'
import { treeData } from '@/utils/index'
import { sysRoleById, sysRoleAssignPrem } from '@/api/setting'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      list: [],
      premissionList: [],
      bol: true,
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    closeEvent() {
      this.$emit('update:isShow', false)
      // this.premissionList = []  静默刷新 重置组件数据,适合所有场景
      // this.bol = false
      // this.$nextTick(() => {
      //   this.bol = true
      // })
      this.$refs.tree.setCheckedKeys([])
    },
    // 获取权限列表
    async getData() {
      const { data } = await sysPermission()
      // console.log(data)
      this.list = treeData(data, '0')
    },
    // 获取当前角色信息
    async getPremissionInfo(id) {
      this.id = id
      const { data } = await sysRoleById(id)
      // this.premissionList = data.permIds
      this.$refs.tree.setCheckedKeys(data.permIds)
      // console.log(data)
    },
    // 分配权限
    async submit() {
      await sysRoleAssignPrem(this.id, this.$refs.tree.getCheckedKeys())
      this.$message.success('分配权限成功')
      this.closeEvent()
    }
  }
}
</script>

<style></style>
